package cn.connectivity.plugin.gui;

import cn.connectivity.plugin.gui.dialog.SettingsSelectDialogWrapper;
import cn.connectivity.plugin.util.Notify;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.validator.routines.UrlValidator;

import javax.swing.*;
import java.awt.event.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Getter
@Setter
public class SettingsForm {

    private JPanel rootPanel;
    private JTextField operatorUrlText;
    private JTextField operatorIdText;
    private JTextField operatorSecretText;
    private JTextField dataSecretText;
    private JTextField dataSecretIVText;
    private JTextField sigSecretText;
    private JTextField partnerOperatorUrlText;
    private JTextField partnerOperatorId;
    private JTextField partnerOperatorSecretText;
    private JTextField partnerDataSecretText;
    private JTextField partnerDataSecretIVText;
    private JTextField partnerSigSecretText;
    private JButton validConfigBtn;
    private JButton validPartnerBtn;
    private JPanel functionPanel;
    private JPanel settingPanel;
    private JPanel contentPanel;
    private JButton loadConfigBtn;

    private Pattern operatorIdPattern = Pattern.compile("[a-zA-Z0-9]{9}");
    private Pattern secretLengthPattern = Pattern.compile("^[a-zA-Z0-9]{16}|[a-zA-Z0-9]{32}|[a-zA-Z0-9]{64}$");

    private Project project;
    private ToolWindow toolWindow;


    public SettingsForm(Project project, ToolWindow toolWindow) {
        this.project = project;
        this.toolWindow = toolWindow;
        FocusListener myUrlfocusListener = new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                String operatorUrlValue = operatorUrlText.getText();
                if(StringUtils.isBlank(operatorUrlValue)) {
                    return;
                }
                String[] schemes = {"http", "https"};
                UrlValidator urlValidator = new UrlValidator(schemes);
                if (!urlValidator.isValid(operatorUrlValue)) {
                    Notify.error("我方URL格式错误", "我方设置的URL:[" + operatorUrlValue + "]格式错误");
                }
            }
        };
        operatorUrlText.addFocusListener(myUrlfocusListener);
        FocusListener partnerUrlfocusListener = new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                String operatorUrlValue = partnerOperatorUrlText.getText();
                if(StringUtils.isBlank(operatorUrlValue)) {
                    return;
                }
                String[] schemes = {"http", "https"};
                UrlValidator urlValidator = new UrlValidator(schemes);
                if (!urlValidator.isValid(operatorUrlValue)) {
                    Notify.error("合作方URL格式错误", "合作方设置的URL:[" + operatorUrlValue + "]格式错误");
                }
            }
        };
        partnerOperatorUrlText.addFocusListener(partnerUrlfocusListener);

        FocusListener myOperatorIdfocusListener = new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                String operatorIdValue = operatorIdText.getText();
                if(StringUtils.isBlank(operatorIdValue)) {
                    return;
                }
                Matcher matcher = operatorIdPattern.matcher(operatorIdValue);
                if (!matcher.matches()) {
                    Notify.error("我方运营商ID长度错误", "我方设置的运营商ID:[" + operatorIdValue + "]长度必须为9位");
                }
            }
        };
        operatorIdText.addFocusListener(myOperatorIdfocusListener);
        FocusListener partnerOperatorIdfocusListener = new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                String operatorIdValue = operatorIdText.getText();
                if(StringUtils.isBlank(operatorIdValue)) {
                    return;
                }
                Matcher matcher = operatorIdPattern.matcher(operatorIdValue);
                if (!matcher.matches()) {
                    Notify.error("合作方运营商ID长度错误", "合作方设置的运营商ID:[" + operatorIdValue + "]长度必须为9位");
                }
            }
        };
        partnerOperatorId.addFocusListener(partnerOperatorIdfocusListener);

        operatorSecretText.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                String operatorSecretValue = operatorSecretText.getText();
                if(StringUtils.isBlank(operatorSecretValue)) {
                    return;
                }
                Matcher matcher = secretLengthPattern.matcher(operatorSecretValue);
                if(!matcher.matches()) {
                    Notify.error("我方运营商密钥长度错误", "我方设置的运营商密钥:[" + operatorSecretValue + "]长度必须为16位或32位或64位");
                }
            }
        });

        partnerOperatorSecretText.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                String partnerOperatorSecretValue = partnerOperatorSecretText.getText();
                if(StringUtils.isBlank(partnerOperatorSecretValue)) {
                    return;
                }
                Matcher matcher = secretLengthPattern.matcher(partnerOperatorSecretValue);
                if(!matcher.matches()) {
                    Notify.error("合作方运营商密钥长度错误", "合作方设置的运营商密钥:[" + partnerOperatorSecretValue + "]长度必须为16位或32位或64位");
                }
            }
        });

        dataSecretText.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                String dataSecretValue = dataSecretText.getText();
                if(StringUtils.isBlank(dataSecretValue)) {
                    return;
                }
                Matcher matcher = secretLengthPattern.matcher(dataSecretValue);
                if(!matcher.matches()) {
                    Notify.error("我方信息密钥长度错误", "我方设置的信息密钥:[" + dataSecretValue + "]长度必须为16位或32位或64位");
                }
            }
        });

        partnerDataSecretText.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                String partnerDataSecretValue = partnerDataSecretText.getText();
                if(StringUtils.isBlank(partnerDataSecretValue)) {
                    return;
                }
                Matcher matcher = secretLengthPattern.matcher(partnerDataSecretValue);
                if(!matcher.matches()) {
                    Notify.error("合作方信息密钥长度错误", "合作方设置的信息密钥:[" + partnerDataSecretValue + "]长度必须为16位或32位或64位");
                }
            }
        });

        dataSecretIVText.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                String dataSecretIVValue = dataSecretIVText.getText();
                if(StringUtils.isBlank(dataSecretIVValue)) {
                    return;
                }
                Matcher matcher = secretLengthPattern.matcher(dataSecretIVValue);
                if(!matcher.matches()) {
                    Notify.error("我方信息密钥偏移量长度错误", "我方设置的信息密钥偏移量:[" + dataSecretIVValue + "]长度必须为16位或32位或64位");
                }
            }
        });

        partnerDataSecretIVText.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                String partnerDataSecretIVValue = partnerDataSecretIVText.getText();
                if(StringUtils.isBlank(partnerDataSecretIVValue)) {
                    return;
                }
                Matcher matcher = secretLengthPattern.matcher(partnerDataSecretIVValue);
                if(!matcher.matches()) {
                    Notify.error("合作方信息密钥偏移量长度错误", "合作方设置的信息密钥偏移量:[" + partnerDataSecretIVValue + "]长度必须为16位或32位或64位");
                }
            }
        });

        sigSecretText.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                String sigSecretValue = sigSecretText.getText();
                if(StringUtils.isBlank(sigSecretValue)) {
                    return;
                }
                Matcher matcher = secretLengthPattern.matcher(sigSecretValue);
                if(!matcher.matches()) {
                    Notify.error("我方签名密钥长度错误", "我方设置的签名密钥:[" + sigSecretValue + "]长度必须为16位或32位或64位");
                }
            }
        });

        partnerSigSecretText.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {

            }

            @Override
            public void focusLost(FocusEvent e) {
                String partnerSigSecretValue = partnerSigSecretText.getText();
                if(StringUtils.isBlank(partnerSigSecretValue)) {
                    return;
                }
                Matcher matcher = secretLengthPattern.matcher(partnerSigSecretValue);
                if(!matcher.matches()) {
                    Notify.error("合作方签名密钥长度错误", "合作方设置的签名密钥:[" + partnerSigSecretValue + "]长度必须为16位或32位或64位");
                }
            }
        });

        // 加载SQLite配置
        loadConfigBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SettingsSelectDialogWrapper selectDialogWrapper = SettingsSelectDialogWrapper.getInstance();
                selectDialogWrapper.show();
            }
        });
    }

}
